// packageC/pages/package/gonzhuangronqiguanli/package_improve/add_improve/add_improve.js
import WxValidate from "../../../../../Wx_Validate/WxValidate"
const mybehavior = require("../../../../../behaviors/requests")
var workshop_options = {

}
var standard_file_options = {

}
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/process_supervision_api",
    fs_upload_url: "https://logisticsengineering.gree.com/process_supervision_api/photo_manage",
    company: "",
    location_data: [],
    supervised_factory: "",  //分厂
    factory_options: [],
    supervised_workshop: "", //车间
    responsible_person: "", //责任人工号
    working_procedure: "", //工序
    problem_description: "",//问题描述
    problem_files: [], //问题图片
    technological_requirements: "", //工艺文件要求
    technological_requirements_key: "",
    technological_requirements_content: "",
    first_class: "", //一级类别
    first_class_options: [],
    total_class_data: [],
    second_class: "", //二级类别
    second_class_options: [],
    deduce_point: "", //扣分
    user_id: "",//提交问题的人员员工号


    choose_img_btn_disabled: false,

    submit_btn: true,
    show_picker_workshop: false,
    columns: [],
    standard_file_columns: [],
    show_picker_StandardFile: false,
    standard_file_content: []
  },

  onClickItem(e) {
    console.log(e);
  },
  // 检测输入值是否为空
  checkNullValue() {
    let that = this
    let data = {
      supervised_factory: this.data.supervised_factory,
      supervised_workshop: this.data.supervised_workshop,
      responsible_person: this.data.responsible_person,
      working_procedure: this.data.working_procedure,
      problem_description: this.data.problem_description,
      problem_files: this.data.problem_files,
      technological_requirements: this.data.technological_requirements,
      first_class: this.data.first_class,
      second_class: this.data.second_class,
      deduce_point: this.data.deduce_point,
    }
    // console.log(data);
    if (!that.WxValidate.checkForm(data)) {
      const error = that.WxValidate.errorList[0]
      // console.log(error);
      that.setData({
        submit_btn: true,
      })
    } else {
      that.setData({
        submit_btn: false,
      })
    }
  },

  // 查询一级类别
  getClassData() {
    let url = this.data.baseUrl + "/problem_punishment_manage/query_problem"
    let data = {
      company: this.data.company,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        console.log(res.data);
        let res_arr = res.data
        let select_data = {}
        for (let i = 0; i < res_arr.length; i++) {
          let children = res_arr[i].children
          let children_arr = []
          if (children.length != 0) {
            for (let j = 0; j < children.length; j++) {
              children_arr.push(children[j].file)
            }
            let obj = {
              [res_arr[i].file]: children_arr
            }
            select_data = Object.assign(select_data, obj)
          }

        }
        this.setData({
          first_class_options: Object.keys(select_data),
          total_class_data: select_data

        })
      } else {

        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {

      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  // 查询扣除分数
  getPointData() {
    let url = this.data.baseUrl + "/problem_punishment_manage/code_point_query"
    let data = {
      company: this.data.company,
      problem_class: this.data.first_class,
      page: 1,
      page_size: 1
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        console.log("查询扣除分数", res);
        this.setData({
          deduce_point: res.data.results[0].point
        })

      } else {

        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        deduce_point: null,
      })

      console.log(err);
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })

  },




  // 获取单位负责人
  getDepartmentUserData() {
    let url = this.data.baseUrl + "/luoyang_supervision_manage/get_process_supervisor"
    let data = {
      company: this.data.company,
      manager: this.data.supervised_factory,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          responsible_person_options: res.data,
        })
      } else {
        this.setData({
          responsible_person_options: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        responsible_person_options: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询后台配置
  getLocationData() {
    let url = this.data.baseUrl + "/location_config_manage/query_location"
    let data = {
      company: this.data.company,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        let res_arr = res.data
        let factory_options = []
        for (let i = 0; i < res_arr.length; i++) {
          factory_options.push(res_arr[i].title)
        }
        this.setData({
          factory_options: factory_options,
          location_data: res_arr

        })
      } else {

        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {

      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 获取工艺文件要求
  getStandardFileData() {
    let url = this.data.baseUrl + "/process_document_qjc_manage/query_standard_file"
    let data = {
      company: this.data.company,
      managers: this.data.supervised_factory,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        let res_arr = res.data
        let select_data = {}
        for (let i = 0; i < res_arr.length; i++) {
          let children = res_arr[i].children
          let children_arr = []
          if (children.length != 0) {
            for (let j = 0; j < children.length; j++) {
              children_arr.push(children[j].file)
            }
            let obj = {
              [res_arr[i].file]: children_arr
            }
            select_data = Object.assign(select_data, obj)
          }

        }
        let columns = []
        if (select_data != {}) {
          columns = [
            {
              values: Object.keys(select_data),
              className: 'column1'
            },
            {
              values: select_data[Object.keys(select_data)[0]],
              className: 'column2',
              defaultIndex: 2
            }
          ]
        }
        standard_file_options = select_data
        let standard_name_l = Object.keys(select_data)
        this.setData({
          standard_file_columns: select_data,
          standard_name_l: standard_name_l
        })

      } else {
        this.setData({

        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({

      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  clickStandardFile() {
    this.setData({
      show_picker_StandardFile: true
    })
  },
  cancelPickerStandardFile() {
    this.setData({
      show_picker_StandardFile: false
    })
  },
  onConfirmPickerStandardFile(e) {
    this.setData({
      show_picker_StandardFile: false,
      technological_requirements: e.detail.value[0] + '-' + e.detail.value[1],
      technological_requirements_key: e.detail.value[0],
      technological_requirements_content: e.detail.value[1],
    })
    this.checkNullValue()
  },
  onChangePickerStandardFile(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(value, index);
    picker.setColumnValues(1, standard_file_options[value[0]]);
  },

  // 工艺文件数据



  // 添加 
  addData() {
    let url = this.data.baseUrl + "/problem_punishment_manage/code_point_add"
    let data = {
      company: this.data.company,
      problem_class: "一级问题测试",
      point: "8",
      // key: "674e5e3933b1d0d5e96049c6",
      // content: "手动功能的手机开发功能但是雷克萨的风景过来看谨慎的乐观数量的法国"
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {

        console.log(res);
      } else {

        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {

      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 下拉选择
  getPickerVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
    if (key == 'technological_requirements_key') {
      let arr = this.data.standard_file_columns
      let content_arr = arr[e.detail]
      this.setData({
        standard_file_content: content_arr
      })


    }
    if (key == 'technological_requirements_content') {
      let res = this.data.technological_requirements_key + '：' + e.detail
      this.setData({
        technological_requirements: res
      })


    }

    if (key == 'supervised_factory') {
      this.setData({
        supervised_workshop: "",
        technological_requirements: "",
        technological_requirements_key: "",
        technological_requirements_content: "",
      })
      this.handleLocation(e.detail)
      this.getDepartmentUserData()
      this.getStandardFileData()
    }
    if (key == 'first_class') {
      let arr = this.data.total_class_data[e.detail]
      this.setData({
        second_class_options: arr,
        second_class: ""
      })
      this.getPointData()
    }
    if (key == 'responsible_person') {
      this.setData({
        responsible_person_str: e.detail.name,
        responsible_person: e.detail._id
      })
    }
    this.checkNullValue()

  },
  // 处理车间数据结构
  handleLocation(e) {
    let key = e
    let location_data = this.data.location_data
    const filtered_data = location_data.filter(location_data => location_data.title == key)
    let select_data = {}
    if (filtered_data[0].children.length != 0) {
      for (let i = 0; i < filtered_data[0].children.length; i++) {
        let children_arr = []
        let filtered_data_children = filtered_data[0].children[i].children
        if (filtered_data_children.length != 0) {
          for (let j = 0; j < filtered_data_children.length; j++) {
            children_arr.push(filtered_data_children[j].title)

          }

          let obj = {
            [filtered_data[0].children[i].title]: children_arr
          }
          select_data = Object.assign(select_data, obj)
        }


      }
    }
    let columns = []
    if (select_data != {}) {
      columns = [
        {
          values: Object.keys(select_data),
          className: 'column1'
        },
        {
          values: select_data[Object.keys(select_data)[0]],
          className: 'column2',
          defaultIndex: 2
        }
      ]
    }

    workshop_options = select_data
    this.setData({
      columns: columns
    })
  },
  clickWorkshop() {
    this.setData({
      show_picker_workshop: true
    })
  },
  cancelPickerWorkshop() {
    this.setData({
      show_picker_workshop: false
    })
  },
  onConfirmPickerWorkshop(e) {
    this.setData({
      show_picker_workshop: false,
      supervised_workshop: e.detail.value[0] + '-' + e.detail.value[1],

    })
    this.checkNullValue()
  },
  onChangePickerWorkshop(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(value, index);
    picker.setColumnValues(1, workshop_options[value[0]]);
  },
  // 处理车间数据


  // 上传照片
  getUploaderVlaue(e) {
    console.log(e);
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
    this.checkNullValue()
  },
  // 提交
  submit() {
    wx.showLoading({
      title: '处理中...',
    })
    let url = this.data.baseUrl + "/luoyang_supervision_manage/submit_process_problem"
    let data = {
      supervised_factory: this.data.supervised_factory,
      supervised_workshop: this.data.supervised_workshop,
      responsible_person: this.data.responsible_person,
      working_procedure: this.data.working_procedure,
      problem_description: this.data.problem_description,
      problem_files: this.data.problem_files,
      technological_requirements: this.data.technological_requirements,
      first_class: this.data.first_class,
      second_class: this.data.second_class,
      deduce_point: this.data.deduce_point,
      user_id: this.data.user_id
    }
    console.log(data);

    this.post(url, data).then(res => {

      if (res.code == 20000) {
        wx.hideLoading()
        wx.showModal({
          title: "提交成功",
          confirmText: '继续提交',
          cancelText: '退出',
          complete: (res) => {
            if (res.cancel) {
              wx.navigateBack()
            }

            if (res.confirm) {
              this.reset()

            }
          }
        })

      } else {
        wx.hideLoading()
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      wx.hideLoading()
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 重置表单
  reset() {
    this.setData({
      supervised_factory: "",  //分厂
      supervised_workshop: "", //车间
      responsible_person: "", //责任人工号
      working_procedure: "", //工序
      problem_description: "",//问题描述
      problem_files: [], //问题图片
      technological_requirements: "", //工艺文件要求
      technological_requirements_key: "",
      technological_requirements_content: "",
      first_class: "", //一级类别
      second_class: "", //二级类别
      deduce_point: "", //扣分
      responsible_person_str: "",
    })
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      supervised_factory: {
        required: true,
      },
      supervised_workshop: {
        required: true,
      },
      responsible_person: {
        required: true,
      },
      working_procedure: {
        required: true,
      },
      problem_description: {
        required: true,
      },
      problem_files: {
        required: true,
      },
      technological_requirements: {
        required: true,
      },
      first_class: {
        required: true,
      },
      second_class: {
        required: true,
      },
      deduce_point: {
        required: true,
      },





    }

    let message = {
      supervised_factory: {
        required: "分厂-不能为空",
      },
      supervised_workshop: {
        required: "车间-不能为空",
      },
      responsible_person: {
        required: "责任人-不能为空",
      },
      working_procedure: {
        required: "工序-不能为空",
      },

      problem_description: {
        required: "问题描述-不能为空",
      },
      problem_files: {
        required: "问题照片-不能为空",
      },
      technological_requirements: {
        required: "工艺文件要求-不能为空",
      },
      first_class: {
        required: "一级类别-不能为空",
      },
      second_class: {
        required: "二级类别-不能为空",
      },
      deduce_point: {
        required: "扣除分数-不能为空",
      },

    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 获取配置数据
  getConfigDatas() {
    let url = this.data.baseUrl + "/get_5s_problem_config"
    let data = {}
    this.post(url, data).then(res => {
      console.log("配置数据", res.data)

      if (res.code == 20000) {
        location_data = res.data.地点
        let arr = [
          {
            values: Object.keys(location_data),
            className: 'column1',
          },
          {
            values: Object.keys(location_data[Object.keys(location_data)[0]]),
            className: 'column2',
          },
          {
            values: location_data[Object.keys(location_data)[0]][Object.keys(location_data[Object.keys(location_data)[0]])[0]],
            className: 'column3',
            defaultIndex: 2,
          }
        ]
        let arr2 = Object.keys(res.data.责任单位)
        let arr3 = Object.keys(res.data.问题类别)

        this.setData({
          columns3: arr3,
          problem_type_list: res.data.问题类别,
          columns2: arr,
          res_boj: res.data.责任单位.内部单位

        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      company: wx.getStorageSync('company'),
      manager: wx.getStorageSync('department'),
      user_id: wx.getStorageSync('_id'),
    })

    // this.addData()
    this.getLocationData()
    this.initValidate()
    // this.getStandardFileData()
    this.getClassData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})